package com.atung.parking.config.utils;

import com.atung.parking.dto.LocationEntity;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.domain.geo.GeoLocation;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author Administrator
 */
@Component
public class ParkingPointOperations {
    public static final String redisGeoCommandsKey = "parkingIot";
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public List<LocationEntity> queryAllParkingIds(Double lon, Double lat) {
        ArrayList parkingIds = new ArrayList();
        Point point = new Point(lon, lat);
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands
                .GeoRadiusCommandArgs
                .newGeoRadiusArgs()
                .includeDistance()
                .includeCoordinates()
                .sortAscending();
        Circle circle = new Circle(point, new Distance(999, Metrics.KILOMETERS));
        GeoResults<RedisGeoCommands.GeoLocation<String>> results = stringRedisTemplate
                .opsForGeo()
                .radius(redisGeoCommandsKey, circle, args);
        results.getContent().forEach(result -> {
            LocationEntity locationEntity = new LocationEntity();
            GeoLocation<String> location = result.getContent();
            Distance distance = result.getDistance();  // 获取距离信息
            Point coordinates = location.getPoint();  // 获取坐标信息
            locationEntity.setLon(coordinates.getX());
            locationEntity.setLat(coordinates.getY());
            locationEntity.setId(Long.valueOf(location.getName()));
            if (distance != null) {
                locationEntity.setDistance(distance.getValue());
            }
            parkingIds.add(locationEntity);
        });
        return parkingIds;
    }

    public List<LocationEntity> queryPageParkingIds(Double lon, Double lat, Long limit) {

        ArrayList parkingIds = new ArrayList();
        Point point = new Point(lon, lat);
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands
                .GeoRadiusCommandArgs
                .newGeoRadiusArgs()
                .includeDistance()
                .includeCoordinates()
                .sortAscending()
                .limit(Objects.isNull(limit) || limit.equals(0L) ? 1 : limit);
        Circle circle = new Circle(point, new Distance(999, Metrics.KILOMETERS));
        GeoResults<RedisGeoCommands.GeoLocation<String>> results = stringRedisTemplate
                .opsForGeo()
                .radius(redisGeoCommandsKey, circle, args);
        results.getContent().forEach(result -> {
            LocationEntity locationEntity = new LocationEntity();
            GeoLocation<String> location = result.getContent();
            Distance distance = result.getDistance();  // 获取距离信息
            Point coordinates = location.getPoint();  // 获取坐标信息
            locationEntity.setLon(coordinates.getX());
            locationEntity.setLat(coordinates.getY());
            locationEntity.setId(Long.valueOf(location.getName()));
            if (distance != null) {
                locationEntity.setDistance(distance.getValue());
            }
            parkingIds.add(locationEntity);
        });
        return parkingIds;
    }
}
